import xlrd
import xlwt
import random
#
# # xlrd实现读取 ==========================================================================================================
# # 处理流程：工作簿----》工作表----》单元格【坐标：行 列】
#
# # 1.打开xls文件，得到一个工作簿对象workbook
# workbook = xlrd.open_workbook(r'Day27-data/student.xls')
#
# # 2.获取工作簿中的工作表名称
# sheetnames = workbook.sheet_names()
# print(sheetnames)
#
# # 3.获取工作表对象sheet
# # 方式一：根据名称获取
# sheet1 = workbook.sheet_by_name('学生表')
# print(sheet1)
# # 方式二：根据索引获取
# sheet2 = workbook.sheet_by_index(2)
# print(sheet2)
# # ----------------------------------------------------------------------------------------------------------------------
#
# # # 4.获取单元格对象cell
# # '''
# # xlrd和xlwt中
# # row:
# #     Excel: 1 2 3 4 5 6.....
# #     Python:0 1 2 3 4 5....
# # col:
# #     Excel: A B C D....
# #     Python:0 1 2 3....
# # '''
# # sheet.cell(row, col):获取某行某列处的单元格对象
# # 需求1：获取A1
# cell1 = sheet1.cell(0,  0)
# print(cell1)  # text:'name'
# # 需求2：获取D3
# cell2 = sheet1.cell(2,  3)
# print(cell2)  # number:23.0
#
# print(type(cell1))
#
# # 5.获取某行的某几列数据
# # a.sheet.row_values(row, col1, col2):前闭后开区间，返回一个列表，其中的元素是数据字符串，col1和col2可以省略
# # 需求1：获取第0行的数据
# print(sheet1.row_values(0))
#
# # 需求2：获取第0行的第0~3列
# print(sheet1.row_values(0, 0, 4))
# #
# # slice:切片
# # b.sheet.row_slice(row, col1, col2)：前闭后开区间，返回一个列表，其中的元素是单元格对象
# print(sheet1.row_slice(0))
# print(sheet1.row_slice(0, 0, 4))
# print('*' * 50)
# #

# xlwt实现写入 ===========================================================================================================

workbook = xlwt.Workbook()
sheet = workbook.add_sheet('学员成绩表')

stu_name = ['张三', '里斯', '王五']
subjects = ['姓名', '语文', '数学', '英语']
scores = [[random.randint(40, 101) for col in range(len(subjects) - 1)] for row in range(len(stu_name))]
print(scores)
for col, value in enumerate(subjects):
    sheet.write(0, col, value)
for row, value in enumerate(stu_name):
    sheet.write(row+1, 0, value)
for row, person in enumerate(scores):
    for col, score in enumerate(person):
        sheet.write(row+1, col+1, score)

workbook.save(r'Day27-data\学员成绩.xls')